package com.bulk.threadPool;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 线程池测试
 *
 * @author 散装java
 */
public class ThreadPoolTest {
    /**
     * CallerRunsPolicy 拒绝策略不会丢弃任务， 会抛给任务的发起方自己执行。
     */
//    static ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 4, 10,
//            TimeUnit.MINUTES, new LinkedBlockingDeque<>(3), new ThreadPoolExecutor.CallerRunsPolicy());
    static ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 4, 10,
            TimeUnit.MINUTES, new LinkedBlockingDeque<>(3), new TaskRejectedHandler());


    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            AtomicInteger atomicInteger = new AtomicInteger(i);
            executor.execute(() -> {
                try {
                    task(atomicInteger.get());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
    }

    public static void task(int i) throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "==" + i + "==");
        TimeUnit.SECONDS.sleep(2);
    }
}
